package com.bw.inspection.page

import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ToastUtils
import com.bw.base.path.RouterPath
import com.bw.common.event.CustomEvent
import com.bw.common.view.BaseMvvmActivity
import com.bw.inspection.R
import com.bw.inspection.adapter.ImageAdapter
import com.bw.inspection.databinding.ActivityAddInspectionBinding
import com.bw.inspection.entity.InspectionEntityItem
import com.bw.inspection.vm.InspectionViewModel
import com.github.gzuliyujiang.wheelpicker.OptionPicker
import com.wildma.pictureselector.PictureBean
import com.wildma.pictureselector.PictureSelector
import dagger.hilt.android.AndroidEntryPoint
import org.greenrobot.eventbus.EventBus

@Route(path = RouterPath.PATH_ADD_INSPECTION_PATH)
@AndroidEntryPoint
class AddInspectionActivity : BaseMvvmActivity<ActivityAddInspectionBinding>() {
    override fun getLayoutId(): Int = R.layout.activity_add_inspection
    private val mViewModel: InspectionViewModel by viewModels()
    private val mAdapter = ImageAdapter()
    override fun initViews() {
        mDataBinding.back.setOnClickListener { finish() }

        mDataBinding.devicesAddressSelect.setOnClickListener {
            val list = mutableListOf("保洁巡检","日常巡检","居家巡检","安防巡检","其他")
            val optionPicker = OptionPicker(this)
            optionPicker.setData(list)
            optionPicker.setTitle("请选择巡检类型")
            optionPicker.setOnOptionPickedListener { position, _ ->
                mDataBinding.devicesAddress.setText(list[ position])
            }
            optionPicker.show()
        }

        mDataBinding.devicesTimeSelect.setOnClickListener {
            val list = mutableListOf("1","2","3","4","其他")
            val optionPicker = OptionPicker(this)
            optionPicker.setData(list)
            optionPicker.setTitle("请选择巡检类型")
            optionPicker.setOnOptionPickedListener { position, _ ->
                mDataBinding.devicesTime.setText(list[ position])
            }
            optionPicker.show()
        }
        mDataBinding.devicesTypeSelect.setOnClickListener {
            val list = mutableListOf("项目1","项目2","项目3","项目4","其他")
            val optionPicker = OptionPicker(this)
            optionPicker.setData(list)
            optionPicker.setTitle("请选择巡检类型")
            optionPicker.setOnOptionPickedListener { position, _ ->
                mDataBinding.devicesType.setText(list[ position])
            }
            optionPicker.show()
        }
        //点击添加图片
        mDataBinding.addImg.setOnClickListener {
            if (mAdapter.data.size<3) {
                PictureSelector.create( this,200).selectPicture()
            }else{
                ToastUtils.showLong("最多添加4张图片")
            }
        }
        mDataBinding.rvImg.layoutManager= GridLayoutManager(this,3)
        mDataBinding.rvImg.adapter=mAdapter
        mDataBinding.rvImg.visibility= View.VISIBLE

    }


    override fun loadData() {
        super.loadData()
        mDataBinding.submitDevice.setOnClickListener {
            val inspectionEntity = InspectionEntityItem(
                id = 0,
                patrolStandard = mDataBinding.devicesTime.text.toString(),
                patrolType = mDataBinding.devicesAddress.text.toString(),
                pointImages = "",
                pointLocation = mDataBinding.devicesContent.text.toString(),
                pointName = mDataBinding.devicesType.text.toString(),
                projectId = "1"
            )
            mViewModel.addInspectionData(inspectionEntity)
        }
    }

    override fun observeDataChanged() {
        super.observeDataChanged()
        mViewModel.mAddInspectionSuccess.observe(this){
            EventBus.getDefault().post(CustomEvent(RouterPath.PATH_INSPECTION_PATH,1,1))
            ToastUtils.showLong("添加成功")
            finish()
        }

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 200 && resultCode == RESULT_OK) {
            val bean =
                data?.extras?.getParcelable<PictureBean>(PictureSelector.PICTURE_RESULT)
            mAdapter.addData(bean?.path!!)
            mAdapter.notifyDataSetChanged()
        }
    }
}